/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.view.retail;

import com.xtd.facade.employment.DepartmentPostFacade;
import com.xtd.facade.inventory.CommodityFacade;
import com.xtd.facade.inventory.InventoryRecordFacade;
import com.xtd.facede.retail.AccountFacade;
import com.xtd.facede.retail.OrderFacade;
import com.xtd.model.inventory.Commodity;
import com.xtd.model.inventory.InventoryRecord;
import com.xtd.model.retail.OrderCommodity;
import com.xtd.model.retail.OrderServer;
import com.xtd.model.retail.ShootingOrder;
import com.xtd.util.CommodityPropertyEnum;
import com.xtd.util.DateTimeUtil;
import com.xtd.view.AbstractManager;
import com.xtd.view.ApplicationBean;
import com.xtd.view.SessionBean;
import com.xtd.view.inventory.CommodityManager;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;

import org.primefaces.event.CloseEvent;

/**
 *
 * @author Lu Yuanbin
 */
@ManagedBean
@SessionScoped
public class OrderManager extends AbstractManager {

    @EJB
    OrderFacade orderFacade;
    @EJB
    AccountFacade accountFacade;
    @EJB
    CommodityFacade commodityFacade;
    @EJB
    DepartmentPostFacade departmentPostFacade;
    @EJB
    InventoryRecordFacade inventoryRecordFacade;
    private List<ShootingOrder> orders;
    private ShootingOrder selectedOrder;
    private String searchContent;
    @ManagedProperty(value = "#{accountManager}")
    private AccountManager accountManager;
    @ManagedProperty(value = "#{commodityManager}")
    private CommodityManager commodityManager;
    @ManagedProperty(value = "#{sessionBean}")
    private SessionBean sessionBean;
    @ManagedProperty(value = "#{applicationBean}")
    private ApplicationBean applicationBean;
    private OrderCommodity selectOrderCommodity;
    private List<OrderCommodity> orderPackageCommodities;
    private Commodity selectedCommodity;
    private int commodityQuantityAddToPackage;
    private List<OrderCommodity> orderCommoditiesForStockout;
    private InventoryRecord inventoryRecord;
    private List<SelectItem> orderCommoditySelectItems;

    /** Creates a new instance of OrderManager */
    public OrderManager() {
    }

    /**
     * @return the orders
     */
    public List<ShootingOrder> getOrders() {
        if (orders == null) {
            orders = new ArrayList();
        }
        return orders;
    }

    /**
     * @param orders the orders to set
     */
    public void setOrders(List<ShootingOrder> orders) {
        this.orders = orders;
    }

    /**
     * @return the selectedOrder
     */
    public ShootingOrder getSelectedOrder() {
        return selectedOrder;
    }

    /**
     * @param selectedOrder the selectedOrder to set
     */
    public void setSelectedOrder(ShootingOrder selectedOrder) {
        this.selectedOrder = selectedOrder;
    }

    /**
     * @return the searchContent
     */
    public String getSearchContent() {
        return searchContent;
    }

    /**
     * @param searchContent the searchContent to set
     */
    public void setSearchContent(String searchContent) {
        this.searchContent = searchContent;
    }

    public void search(ActionEvent actionEvent) {
        this.orders.clear();
        this.orders.addAll(orderFacade.find(searchContent));
        print("size = " + this.orders.size());
    }

    public void createOrder(ActionEvent actionEvent) {
        this.selectedOrder = new ShootingOrder(orderFacade.generateNumber(), getSessionBean().getUser());
    }

    private boolean validateOrderCommodity(OrderCommodity orderCommodity) {
        if (orderCommodity.hasStockOutItem() && selectOrderCommodity.isCancelled()) {
            addCallbackParam("success", false);
            addMessage("保存失败", orderCommodity.getName() + "已出库,不可取消。", FacesMessage.SEVERITY_WARN);
            return false;
        } else if ((!orderCommodity.isRetrieved() && orderCommodity.getReturnedQuantity() > 0)) {
            addCallbackParam("success", false);
            addMessage("保存失败", orderCommodity.getName() + "未取件，不可退货。", FacesMessage.SEVERITY_WARN);
            return false;
        } else if (orderCommodity.getReturnedQuantity() <= 0 && orderCommodity.getReturnedPrice().compareTo(BigDecimal.ONE) > 0) {
            addCallbackParam("success", false);
            addMessage("保存失败", orderCommodity.getName() + "未退库，不可填写退货金额。", FacesMessage.SEVERITY_WARN);
            return false;
        } else if (BigDecimal.valueOf(orderCommodity.getReturnedQuantity()).multiply(orderCommodity.getUnitPrice()).compareTo(orderCommodity.getReturnedPrice()) < 0) {
            addCallbackParam("success", false);
            addMessage("保存失败", orderCommodity.getName() + "退货金额不可超出购买金额。", FacesMessage.SEVERITY_WARN);
            return false;
        } else if (orderCommodity.getProperty().equals(CommodityPropertyEnum.套系商品) && orderCommodity.getReturnedQuantity() > 0) {
            addCallbackParam("success", false);
            addMessage("保存失败", "套系商品。", FacesMessage.SEVERITY_WARN);
            return false;
        } else if (orderCommodity.getProperty().equals(CommodityPropertyEnum.零售商品) && orderCommodity.getReturnedQuantity() > orderCommodity.getQuantity() - orderCommodity.getStockout()) {
            addCallbackParam("success", false);
            addMessage("保存失败", "需将零售商品退库，方可退货", FacesMessage.SEVERITY_WARN);
            return false;
        }
        return true;
    }

    public void saveOrderCommodity(ActionEvent actionEvent) {
        boolean valid = true;
        for (OrderServer os : selectOrderCommodity.getOrderServerCollection()) {
            if (os.getIntroducerRoyalty().compareTo(os.getPriceRoyalty()) > 0) {
                os.setIntroducerRoyalty(BigDecimal.ZERO);
                valid = false;
            }
        }
        if (!valid) {
            addCallbackParam("success", false);
            addMessage("保存失败", "介绍人提成不可超过金额提成", FacesMessage.SEVERITY_WARN);
            return;
        }
        addMessage("保存成功", null, FacesMessage.SEVERITY_INFO);
    }

    public void saveOrderCommoditesForRetrieve(ActionEvent actionEvent) {
        for (OrderCommodity oc : getOrderCommoditesForRetrieve()) {
            if (oc.isRetrieved()) {
                if (oc.isStock() && oc.getQuantity() - oc.getReturnedQuantity() != oc.getStockout()) {
                    addCallbackParam("success", false);
                    addMessage("保存失败", oc.getName() + "尚有未出库商品", FacesMessage.SEVERITY_WARN);
                    return;
                } else {
                    if (oc.getProperty().equals(CommodityPropertyEnum.加工商品) && !oc.getCompleted()) {
                        addCallbackParam("success", false);
                        addMessage("保存失败", oc.getName() + "尚未完工", FacesMessage.SEVERITY_WARN);
                        return;
                    }
                }
            }
        }
        addCallbackParam("success", true);
        addMessage("保存成功", null, FacesMessage.SEVERITY_INFO);
    }

    public void saveMaterial(ActionEvent actionEvent) {

        for (OrderCommodity oc : selectOrderCommodity.getSubOrderCommodityCollection()) {
            if (oc.getQuantity() < oc.getStockout()) {
                addCallbackParam("success", false);
                addMessage("保存失败", "数量不可小于出库输", FacesMessage.SEVERITY_WARN);
                return;
            }
            if (selectOrderCommodity.getCompleted()) {
                if (oc.getQuantity() - oc.getReturnedQuantity() != oc.getStockout()) {
                    addCallbackParam("success", false);
                    addMessage("保存失败", "尚有未出库材料，不可完工。", FacesMessage.SEVERITY_WARN);
                    return;
                }
            }
        }
        addMessage("保存成功", null, FacesMessage.SEVERITY_INFO);
        addCallbackParam("success", true);
    }

    public void save(ActionEvent actionEvent) {

        RequestContext context = RequestContext.getCurrentInstance();

        if (selectedOrder.getAccount().getId() == null) {
            List existingAccounts = accountFacade.findDuplicatePhone(selectedOrder.getAccount());
            if (existingAccounts != null && existingAccounts.size() > 0) {
                getAccountManager().getAccounts().clear();
                getAccountManager().getAccounts().addAll(existingAccounts);
                context.addCallbackParam("duplicate", true);
                addMessage("保存失败", "请从弹出窗口处，选定已有客户。", FacesMessage.SEVERITY_WARN);
                return;
            }
        }
        boolean isNew = selectedOrder.isNew();
        if (selectedOrder.isCancelled()) {
            if (isNew) {
                context.addCallbackParam("success", false);
                addMessage("保存失败", "新增订单不可取消。", FacesMessage.SEVERITY_WARN);
                return;
            } else if (selectedOrder.getTotalExpense().compareTo(selectedOrder.getTotalPayment()) != 0) {
                context.addCallbackParam("success", false);
                addMessage("保存失败", "结余尚未清算不可取消。", FacesMessage.SEVERITY_WARN);
                return;
            } else if (selectedOrder.hasStockoutCommodity()) {
                context.addCallbackParam("success", false);
                addMessage("保存失败", "尚有出库商品未退库。", FacesMessage.SEVERITY_WARN);
                return;
            } else if (selectedOrder.getSettledTime() != null) {
                context.addCallbackParam("success", false);
                addMessage("保存失败", "此单已结清。", FacesMessage.SEVERITY_WARN);
                return;
            }
            for (OrderCommodity oc : selectOrderCommodity.getSubOrderCommodityCollection()) {
                oc.setCommodity(null);
                for (OrderCommodity sub : oc.getSubOrderCommodityCollection()) {
                    oc.setCommodity(null);
                    for (OrderCommodity material : oc.getSubOrderCommodityCollection()) {
                        material.setCommodity(null);
                    }
                }
            }
            context.addCallbackParam("success", true);
        }

        try {
            orderFacade.update(selectedOrder, this.getSessionBean().getUser());
            addMessage("保存成功", null);
            context.addCallbackParam("success", false);
            if (isNew) {
                this.orders.add(selectedOrder);
            }
        } catch (Exception e) {
            getLogger(this.getClass()).log(Level.FINEST, "订单更新失败", e);
            addMessage("保存失败", "请稍候再试", FacesMessage.SEVERITY_ERROR);
            context.addCallbackParam("success", false);
        }
    }

    public void settle(ActionEvent actionEvent) {
        if (!selectedOrder.isBalanced()) {
            addCallbackParam("success", false);
            addMessage("无法结单", "付款金额错误", FacesMessage.SEVERITY_ERROR);
            return;
        } else {
            for (OrderCommodity oc : selectedOrder.getOrderCommodityCollection()) {
                if (oc.isPackaged()) {
                    for (OrderCommodity sub : oc.getSubOrderCommodityCollection()) {
                        if (sub.isRetrievable() && !sub.isRetrieved()) {
                            addCallbackParam("success", false);
                            addMessage("无法结单", sub.getName() + "尚有未取件商品", FacesMessage.SEVERITY_ERROR);
                            return;
                        }
                    }
                } else if (oc.isRetrievable() && !oc.isRetrieved()) {
                    addCallbackParam("success", false);
                    addMessage("无法结单", oc.getName() + "尚有未取件", FacesMessage.SEVERITY_ERROR);
                    return;
                }
            }
        }

        selectedOrder.setSettled(true);
        if (selectedOrder.getSettledTime() == null) {
            selectedOrder.setSettledTime(new Date());
            for (OrderCommodity oc : selectedOrder.getOrderCommodityCollection()) {
                oc.setCommodity(null);
                for (OrderServer os : oc.getOrderServerCollection()) {
                    os.setSettledTime(selectedOrder.getSettledTime());
                }
                for (OrderCommodity sub : oc.getSubOrderCommodityCollection()) {
                    oc.setCommodity(null);
                    for (OrderServer subos : sub.getOrderServerCollection()) {
                        subos.setSettledTime(selectedOrder.getSettledTime());
                    }
                    for (OrderCommodity material : oc.getSubOrderCommodityCollection()) {
                        material.setCommodity(null);
                    }
                }
            }
        }
        try {
            orderFacade.update(selectedOrder, this.getSessionBean().getUser());
            addMessage("操作成功", "已结单");
            addCallbackParam("success", true);
        } catch (Exception e) {
            getLogger(this.getClass()).log(Level.FINEST, "订单更新失败", e);
            addMessage("操作失败", "请稍候再试", FacesMessage.SEVERITY_ERROR);
        }

    }

    public void cancelSettle(ActionEvent actionEvent) {
        selectedOrder.setSettled(false);
        try {
            orderFacade.update(selectedOrder, this.getSessionBean().getUser());
            addMessage("保存成功", null);
            addCallbackParam("success", true);
        } catch (Exception e) {
            getLogger(this.getClass()).log(Level.FINEST, "订单更新失败", e);
            addMessage("保存失败", "请稍候再试", FacesMessage.SEVERITY_ERROR);
        }
    }

    public void cancellCreate(CloseEvent actionEvent) {
        addMessage("取消保存", null);
    }

    public void findToday(ActionEvent actionEvent) {

        this.orders.clear();
        this.orders.addAll(this.orderFacade.find(DateTimeUtil.getDateBefore(new Date(), 0), DateTimeUtil.getDateEnd(new Date())));
    }

    public void findWeek(ActionEvent actionEvent) {

        this.orders.clear();
        this.orders.addAll(this.orderFacade.find(DateTimeUtil.getWeekStart(), DateTimeUtil.getDateEnd(new Date())));
    }

    public void findMonth(ActionEvent actionEvent) {

        this.orders.clear();
        this.orders.addAll(this.orderFacade.find(DateTimeUtil.getMonthStart(), DateTimeUtil.getDateEnd(new Date())));
    }

    public void findUnFinishedOrder(ActionEvent actionEvent) {

        this.orders.clear();
        this.orders.addAll(this.orderFacade.findUnFinishedOrder());
    }

    /**
     * @return the accountManager
     */
    public AccountManager getAccountManager() {
        return accountManager;
    }

    /**
     * @param accountManager the accountManager to set
     */
    public void setAccountManager(AccountManager accountManager) {
        this.accountManager = accountManager;
    }

    public String addCommodity(Commodity c) {
        selectedOrder.getOrderCommodityCollection().add(new OrderCommodity(selectedOrder, c,this.sessionBean.getUser()));
        return null;
    }

    public String addCommodityToPackage(Commodity c) {
        RequestContext context = RequestContext.getCurrentInstance();
        if (c.isPackage()) {
            context.addCallbackParam("success", false);
            addMessage("添加失败", "不可添加套系商品", FacesMessage.SEVERITY_ERROR);
        } else if (getOrderPackageCommodities().isEmpty()) {
            context.addCallbackParam("success", false);
            addMessage("添加失败", "没有套系商品可以添加", FacesMessage.SEVERITY_ERROR);
        } else {
            selectedCommodity = c;
            context.addCallbackParam("success", true);
            addMessage("提示", "请从弹出窗口选定套系", FacesMessage.SEVERITY_ERROR);
        }

        return null;
    }

    public String addMaterial(Commodity c) {
        RequestContext context = RequestContext.getCurrentInstance();
        if (getWorkingCommodities().isEmpty()) {
            context.addCallbackParam("success", false);
            addMessage("添加失败", "没有加工商品可以添加材料", FacesMessage.SEVERITY_ERROR);
        } else {
            selectedCommodity = c;
            context.addCallbackParam("success", true);
            addMessage("提示", "请从弹出窗口选定虚拟商品", FacesMessage.SEVERITY_ERROR);
        }
        return null;
    }

    public List<OrderCommodity> getWorkingCommodities() {
        List virtualCommodities = new ArrayList();
        if (selectedOrder != null) {
            for (OrderCommodity oc : selectedOrder.getOrderCommodityCollection()) {
                if (!oc.isCancelled() && oc.getProperty().equals(CommodityPropertyEnum.加工商品)) {
                    virtualCommodities.add(oc);
                } else if (oc.getProperty().equals(CommodityPropertyEnum.套系商品)) {
                    for (OrderCommodity suboc : oc.getSubOrderCommodityCollection()) {
                        if (!suboc.isCancelled() && suboc.getProperty().equals(CommodityPropertyEnum.加工商品)) {
                            virtualCommodities.add(suboc);
                        }
                    }
                }
            }
        }
        return virtualCommodities;
    }

    public String deleteCommodity(OrderCommodity orderCommodity) {
        if (orderCommodity.hasStockOutRecord()) {
            addMessage("删除失败", "已有商品或材料出库");
            return null;
        }
        if (orderCommodity.getCreatedBy().equals(this.sessionBean.getUser())) {
            addMessage("删除失败", "无法删除其他人添加的商品");
            return null;
        }
        selectedOrder.getOrderCommodityCollection().remove(orderCommodity);
        return null;
    }

    public String deleteMaterial(OrderCommodity orderCommodity) {

        if (orderCommodity.hasStockOutRecord()) {
            addMessage("删除失败", "已有材料出库");
            return null;
        }
        selectOrderCommodity.getSubOrderCommodityCollection().remove(orderCommodity);
        return null;
    }

    public String editOrderCommodity(OrderCommodity orderCommodity) {
        if (!orderCommodity.getCreatedBy().equals(this.sessionBean.getUser())) {
            addCallbackParam("success", false);
            addMessage("删除失败", "无法修改其他人添加的商品");
            return null;
        }
        selectOrderCommodity = orderCommodity;
        orderCommoditySelectItems = new ArrayList();
        orderCommoditySelectItems.add(new SelectItem(selectOrderCommodity, selectOrderCommodity.getName()));
        if (selectOrderCommodity.getProperty().equals(CommodityPropertyEnum.套系商品)) {
            for (OrderCommodity sub : selectOrderCommodity.getSubOrderCommodityCollection()) {
                orderCommoditySelectItems.add(new SelectItem(sub, sub.getName()));
            }
        }
        addCallbackParam("success", true);
        return null;
    }

    public String editWorkingCommodity(OrderCommodity orderCommodity) {
        selectOrderCommodity = orderCommodity;
        return null;
    }

    public List<SelectItem> getOrderCommoditySelectItems() {
        return orderCommoditySelectItems;
    }

    public void setOrderCommoditySelectItems(List<SelectItem> orderCommoditySelectItems) {
        this.orderCommoditySelectItems = orderCommoditySelectItems;
    }

    public String stockOut(OrderCommodity orderCommodity, boolean stock_type) {

        if (selectedOrder.isSettled()) {
            addCallbackParam("success", false);
            addMessage("操作失败", "此单已结清，不可再对其进行操作", FacesMessage.SEVERITY_WARN);
            return null;
        }
        if (orderCommodity.isRetrieved() || (orderCommodity.getSuperOrderCommodity() != null && (orderCommodity.getSuperOrderCommodity().isRetrieved() || orderCommodity.getSuperOrderCommodity().getCompleted()))) {
            addCallbackParam("success", false);
            addMessage("操作失败", "该商品已经领取或完工。", FacesMessage.SEVERITY_WARN);
            return null;
        }

        addCallbackParam("success", true);
        inventoryRecord = new InventoryRecord(orderCommodity, stock_type, this.getSessionBean().getUser());
        return null;
    }

    public void confirmStockOut(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();
        if (inventoryRecord.getStockType()) {

            if (inventoryRecord.getOrderCommodity().getStockout() < inventoryRecord.getQuantity()) {
                context.addCallbackParam("success", false);
                addMessage("确认失败", "入库数量超出", FacesMessage.SEVERITY_WARN);
                return;
            }

            inventoryRecord.getOrderCommodity().setStockout(inventoryRecord.getOrderCommodity().getStockout() - inventoryRecord.getQuantity());
        } else {

            if (inventoryRecord.getQuantity() + inventoryRecord.getOrderCommodity().getStockout() > inventoryRecord.getOrderCommodity().getQuantity() - inventoryRecord.getOrderCommodity().getReturnedQuantity()) {
                context.addCallbackParam("success", false);
                addMessage("确认失败", "出库数量超出", FacesMessage.SEVERITY_WARN);
                return;
            }
            inventoryRecord.getOrderCommodity().setStockout(inventoryRecord.getOrderCommodity().getStockout() + inventoryRecord.getQuantity());
        }

        inventoryRecordFacade.confirmOrderCommodityStock(inventoryRecord);
        context.addCallbackParam("success", true);
        addMessage("确认成功", null, FacesMessage.SEVERITY_WARN);
    }

    /**
     * @return the commodityManager
     */
    public CommodityManager getCommodityManager() {
        return commodityManager;
    }

    /**
     * @param commodityManager the commodityManager to set
     */
    public void setCommodityManager(CommodityManager commodityManager) {
        this.commodityManager = commodityManager;
    }

    /**
     * @return the selectedPackageCommodityId
     */
    /**
     * @return the sessionBean
     */
    public SessionBean getSessionBean() {
        return sessionBean;
    }

    /**
     * @param sessionBean the sessionBean to set
     */
    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    /**
     * @return the serverSelectItems
     */
    public List<SelectItem> getServerSelectItems() {
        OrderServer os = (OrderServer) getBean("orderServer");

        return this.applicationBean.getEmployeeSelectItemMap().get((os.getDepartpost() == null ? 0 : os.getDepartpost().getId()));
    }

    /**
     * @return the applicationBean
     */
    public ApplicationBean getApplicationBean() {
        return applicationBean;
    }

    /**
     * @param applicationBean the applicationBean to set
     */
    public void setApplicationBean(ApplicationBean applicationBean) {
        this.applicationBean = applicationBean;
    }

    /**
     * @return the orderPackageCommodities
     */
    public List<OrderCommodity> getOrderPackageCommodities() {
        orderPackageCommodities = new ArrayList();
        if (selectedOrder != null) {
            for (OrderCommodity oc : selectedOrder.getOrderCommodityCollection()) {
                if (oc.isPackaged()) {
                    orderPackageCommodities.add(oc);
                }
            }
        }
        return orderPackageCommodities;
    }

    public List<OrderCommodity> getOrderCommoditesForRetrieve() {
        List orderCommodities = new ArrayList();
        if (selectedOrder != null) {
            for (OrderCommodity oc : selectedOrder.getOrderCommodityCollection()) {
                if (oc.getProperty().equals(CommodityPropertyEnum.加工商品) || oc.getProperty().equals(CommodityPropertyEnum.零售商品) || oc.getProperty().equals(CommodityPropertyEnum.材料商品)) {
                    orderCommodities.add(oc);
                }
                if (oc.isPackaged()) {
                    for (OrderCommodity sub : oc.getSubOrderCommodityCollection()) {
                        if (sub.getProperty().equals(CommodityPropertyEnum.加工商品) || sub.getProperty().equals(CommodityPropertyEnum.零售商品) || sub.getProperty().equals(CommodityPropertyEnum.材料商品)) {
                            orderCommodities.add(sub);
                        }
                    }
                }
            }
        }
        return orderCommodities;
    }

    public List<OrderCommodity> getOrderCommoditiesForStockout() {
        orderCommoditiesForStockout = new ArrayList();
        if (selectedOrder != null) {
            for (OrderCommodity oc : selectedOrder.getOrderCommodityCollection()) {
                if (oc.isStock()) {
                    orderCommoditiesForStockout.add(oc);
                }
                for (OrderCommodity sub : oc.getSubOrderCommodityCollection()) {
                    if (sub.isStock()) {
                        orderCommoditiesForStockout.add(sub);
                    } else {
                        for (OrderCommodity material : sub.getSubOrderCommodityCollection()) {
                            orderCommoditiesForStockout.add(material);
                        }
                    }
                }
            }
        }
        return orderCommoditiesForStockout;
    }

    /**
     * @param orderPackageCommodities the orderPackageCommodities to set
     */
    public void setOrderPackageCommodities(List<OrderCommodity> orderPackageCommodities) {
        this.orderPackageCommodities = orderPackageCommodities;
    }

    /**
     * @return the selectedCommodity
     */
    public Commodity getSelectedCommodity() {
        return selectedCommodity;
    }

    /**
     * @param selectedCommodity the selectedCommodity to set
     */
    public void setSelectedCommodity(Commodity selectedCommodity) {
        this.selectedCommodity = selectedCommodity;
    }

    public void selectPackageToAdd(OrderCommodity orderCommodity) {
        orderCommodity.getSubOrderCommodityCollection().add(new OrderCommodity(orderCommodity, commodityQuantityAddToPackage, selectedCommodity,this.sessionBean.getUser()));
    }

    /**
     * @return the commodityQuantityAddToPackage
     */
    public int getCommodityQuantityAddToPackage() {
        return commodityQuantityAddToPackage;
    }

    /**
     * @param commodityQuantityAddToPackage the commodityQuantityAddToPackage to set
     */
    public void setCommodityQuantityAddToPackage(int commodityQuantityAddToPackage) {
        this.commodityQuantityAddToPackage = commodityQuantityAddToPackage;
    }

    /**
     * @return the selectOrderCommodity
     */
    public OrderCommodity getSelectOrderCommodity() {
        return selectOrderCommodity;
    }

    /**
     * @param selectOrderCommodity the selectOrderCommodity to set
     */
    public void setSelectOrderCommodity(OrderCommodity selectOrderCommodity) {
        this.selectOrderCommodity = selectOrderCommodity;
    }

    /**
     * @return the inventoryRecord
     */
    public InventoryRecord getInventoryRecord() {
        return inventoryRecord;
    }

    /**
     * @param inventoryRecord the inventoryRecord to set
     */
    public void setInventoryRecord(InventoryRecord inventoryRecord) {
        this.inventoryRecord = inventoryRecord;
    }

    /**
     * @return the stockQuantityLabel
     */
    public String getStockQuantityLabel() {
        if (inventoryRecord == null) {
            return "";
        }
        if (inventoryRecord.getStockType()) {
            return "入库数量";
        } else {
            return "出库数量";
        }

    }
}
